package com.zhihao.baobao.recketdemo;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;

/**
 * �����򵥶���
 * 
 * @author wissea
 * 
 */
public class MainActivity extends Activity {
	private Rocket rocket;

	private ImageView rocket_fly;
	private ImageView cloud;
	private ImageView rocket_line;
	private MediaPlayer mediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		rocket_fly = (ImageView) findViewById(R.id.fly_rocket);
		cloud = (ImageView) findViewById(R.id.cloud);
		rocket_line = (ImageView) findViewById(R.id.cloud_line);
		rocket = (Rocket) findViewById(R.id.rocket);
		final TranslateAnimation animation = new TranslateAnimation(0, 0, 0,
				-getWindowManager().getDefaultDisplay().getHeight());
		animation.setDuration(1000);
		animation.setFillAfter(true);
		animation.setInterpolator(new AccelerateInterpolator());

		final AlphaAnimation alphaAnimation3 = new AlphaAnimation(1.0f, 0.0f);
		alphaAnimation3.setDuration(1000);
		alphaAnimation3.setFillAfter(true);

		final AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
		alphaAnimation.setDuration(500);

		final AlphaAnimation alphaAnimation2 = new AlphaAnimation(0.1f, 1.0f);
		alphaAnimation2.setDuration(500);
		alphaAnimation2.setStartOffset(300);

		alphaAnimation3.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				Toast.makeText(getApplicationContext(), "С����ҵ�̫��,�Һ�С���Ƕ�������...",
						Toast.LENGTH_SHORT).show();
				rocket_fly.setVisibility(View.GONE);
				cloud.setVisibility(View.GONE);
				rocket_line.setVisibility(View.GONE);
				rocket.reset();
			}
		});
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				mediaPlayer = MediaPlayer.create(MainActivity.this,
						R.raw.rocket);
				mediaPlayer.start();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mediaPlayer.stop();
				mediaPlayer.release();
				cloud.startAnimation(alphaAnimation3);
				rocket_line.startAnimation(alphaAnimation3);
			}
		});

		rocket.setOnChangeListener(new LocationChangeListener() {
			@Override
			public void onchange() {
				rocket_fly.setVisibility(View.VISIBLE);
				cloud.setVisibility(View.VISIBLE);
				rocket_line.setVisibility(View.VISIBLE);
				rocket_fly.startAnimation(animation);
				cloud.startAnimation(alphaAnimation);
				rocket_line.startAnimation(alphaAnimation2);
			}
		});

	}

}
